home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / QuickTime / Show Movie / Sources / MoviePrefs.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  14.0 KB  |  494 lines  |  [TEXT/MPCC]

  1. /*
  2.   File:            MoviePrefs.c
  3.   Contains:         movie handling routines
  4.   Written by:    Jason Hodges-Harris & Don Swatman
  5.   Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  6. */
  7.  
  8. //==============================================
  9. // Preference dialog used by each movie             
  10. //==============================================
  11.  
  12. #include <Dialogs.h>
  13. #include <TextUtils.h>
  14.  
  15. #include "MoviePrefs.h"
  16.  
  17. #include "MovieStuff.h"
  18.  
  19.  
  20. //==============================================
  21. // Global Stuff, init and tear down             
  22. //==============================================
  23.  
  24. // Prototypes of functions  used in UPPs
  25. pascal void DrawDLOGFrameRect(WindowPtr theDialog, short itemNo);
  26.  
  27. // Global UPPs
  28. UserItemUPP     gDLOGFrameUpp;
  29.  
  30. // Globals
  31. MovieOptionsType gDefaultMoviePrefs;
  32.  
  33. //----------------------------------------------
  34. // InitMoviePrefs
  35. //
  36. // Init's any globals used in MovieStuff
  37. // i.e. the UPPs             
  38. //----------------------------------------------
  39. void InitMoviePrefs(void)
  40. {
  41.     gDLOGFrameUpp = nil;
  42. // Create Dialog Frame Upp for "DrawDLOGFrameRect"
  43.     gDLOGFrameUpp = NewUserItemProc( DrawDLOGFrameRect );
  44.  
  45. // Set up the default movie prefs
  46.     SetUpDefaultMoviePref( &gDefaultMoviePrefs );
  47. }
  48.  
  49. //----------------------------------------------
  50. // KillMoviePrefs
  51. //
  52. // Removes the UPPs             
  53. //----------------------------------------------
  54. void KillMoviePrefs(void)
  55. {
  56. // Clear the Universal Proc Pointers
  57. //    You don't need to do this as quiting the app will do it for you,
  58. //    however, I have this thing about neatness.    
  59.     if (gDLOGFrameUpp)
  60.         DisposeRoutineDescriptor(gDLOGFrameUpp);
  61.         
  62. }
  63.  
  64. //----------------------------------------------
  65. //  SetUpDefaultMoviePref 
  66. //
  67. // Puts the standard options into a MovieOptionsType
  68. // preference record                                  
  69. //----------------------------------------------
  70. void SetUpDefaultMoviePref ( MovieOptionsType *theOptions )
  71. {
  72.     (*theOptions).closeAtEnd      = kUserCloseWind;       // User has to close the window
  73.     (*theOptions).hasController   = kHasMovieController;  // Movie has a controller
  74.     (*theOptions).do20to10Loop    = false;     // Don't do the loop stuff
  75.     (*theOptions).loopFrom        = 20;        // Set loop to start at 20 secs
  76.     (*theOptions).loopTo          = 10;        //    and loop to 10 secs
  77.     (*theOptions).rateChangeDelay = kInSync;   // No rate change stuff
  78.     (*theOptions).slaveAheadBy    = kInSync;   // Slave is in sync with master
  79.     (*theOptions).slaveStartDelay = kInSync;   // Slave starts at the same time as the master
  80. }
  81.  
  82. //==============================================
  83. // Usefull dialog item stuff
  84. //==============================================
  85.  
  86. //----------------------------------------------
  87. //  InstallCustomDlogItem                                   
  88. //----------------------------------------------
  89.  
  90. void InstallCustomDlogItem( DialogPtr theDialog,
  91.                                                         short     itemNum,
  92.                                                         UserItemUPP customProcUpp );
  93. void InstallCustomDlogItem( DialogPtr theDialog,
  94.                                                         short     itemNum,
  95.                                                         UserItemUPP customProcUpp )
  96. {
  97.     short     itemType;
  98.     Handle    itemHandle;
  99.     Rect      itemRect;
  100.  
  101.     GetDialogItem(theDialog, itemNum, &itemType, &itemHandle, &itemRect);
  102.     SetDialogItem(theDialog, itemNum, itemType, (Handle)customProcUpp, &itemRect);
  103. }
  104.  
  105. //----------------------------------------------
  106. //  DrawDLOGFrameRect
  107. //
  108. // Dialog custom draw item. It draws a frame round the 
  109. // items's rect so it can be used to frame areas                              
  110. //----------------------------------------------
  111.  
  112. pascal void DrawDLOGFrameRect(WindowPtr theDialog, short itemNo)
  113. {
  114.     short     itemType;   //returned item type
  115.     Rect      itemRect;   //returned display rectangle
  116.     Handle    itemHandle; //returned item handle
  117.     WindowPtr oldPort;
  118.     PenState  curPen;
  119.  
  120. // Get information about the item ( including it's rect )
  121.     GetDialogItem(theDialog, itemNo, &itemType, &itemHandle, &itemRect);
  122.  
  123. // Save the current port and the pen's state
  124.     GetPort(&oldPort);
  125.     SetPort( theDialog );
  126.     GetPenState(&curPen);
  127.  
  128. // Draw the frame with a normal pen
  129.     PenNormal();
  130.     FrameRect( &itemRect );
  131.  
  132. // Reset the pen state and the port
  133.     SetPenState(&curPen);
  134.     SetPort(oldPort);
  135. }
  136.  
  137. //==============================================
  138. //  Movie Preferences                                   
  139. //==============================================
  140.  
  141. //----------------------------------------------
  142. // Constants used in the dialog box
  143. //----------------------------------------------
  144.  
  145. #define kOneMovieID 9000
  146.  
  147. enum {
  148.         kOkButton = 1,
  149.         kCancelButton,
  150.         kTitleStatic,
  151.         kHelpBox,
  152.         kHelpTitle,
  153.         kHelpText,
  154.         kCloseAtEnd,
  155.         kHasController,
  156.         kDoTheLoop,
  157.         kRateChangePU,
  158.         kSlaveAheadPU,
  159.         kSlaveDelayStartPU,
  160.         kSlaveBox,
  161.         kSlaveTitle
  162. };
  163.  
  164. //----------------------------------------------
  165. // ConvertTimeToMenuItem
  166. //
  167. // Converts one of the time values into the pop up menu's
  168. // selected item.
  169. //----------------------------------------------
  170.  
  171. short ConvertTimeToMenuItem( short time );
  172. short ConvertTimeToMenuItem( short time )
  173. {
  174.     short menuItem = 1; // default the item to 1, normally kInSync
  175.     
  176.     switch (time)
  177.     {
  178.     case (kInSync):
  179.         menuItem = 1;
  180.         break;
  181.     case (kOneThird):
  182.         menuItem = 2;
  183.         break;
  184.     case (10):
  185.         menuItem = 3;
  186.         break;
  187.     }
  188.     return ( menuItem );
  189. }
  190.  
  191. //----------------------------------------------
  192. // ConvertMenuItemToTime
  193. //
  194. // Converts a pop up menu's selected item into
  195. // one of the time values
  196. //----------------------------------------------
  197.  
  198. short ConvertMenuItemToTime( short menuItem );
  199. short ConvertMenuItemToTime( short menuItem )
  200. {
  201.     short time = kInSync;  // default to kInSync (i.e. don't do anything)
  202.     
  203.     switch (menuItem)
  204.     {
  205.     case (1):
  206.         time = kInSync;
  207.         break;
  208.     case (2):
  209.         time = kOneThird;
  210.         break;
  211.     case (3):
  212.         time = 10;
  213.         break;
  214.     }
  215.     return ( time );
  216. }
  217.  
  218. //----------------------------------------------
  219. // SetUpOneMovieDLOG
  220. //
  221. // Sets up the items in the preferences dialog
  222. // depending on "MovieOptionsType *theOptions"
  223. // In general, each chunk get's the control handle from
  224. // a dialog item and set's it up appropriately
  225. //----------------------------------------------
  226.  
  227. void SetUpOneMovieDLOG( DialogPtr oneMovieDialog,
  228.                                                 MovieOptionsType *theOptions,
  229.                                                 Boolean hasSlaveMovie );
  230.  
  231. void SetUpOneMovieDLOG( DialogPtr oneMovieDialog,
  232.                                                 MovieOptionsType *theOptions,
  233.                                                 Boolean hasSlaveMovie )
  234. {
  235.     short     itemType;
  236.     Handle    item;
  237.     Rect      box;
  238.     short     newPUValue;
  239.  
  240. // Set up the frame boxes custom items
  241.     InstallCustomDlogItem( oneMovieDialog, kHelpBox, gDLOGFrameUpp );
  242.     InstallCustomDlogItem( oneMovieDialog, kSlaveBox, gDLOGFrameUpp );
  243.  
  244. // kCloseAtEnd from closeAtEnd
  245.     GetDItem( oneMovieDialog, kCloseAtEnd, &itemType, &item, &box);
  246.     if ((*theOptions).closeAtEnd)
  247.         SetControlValue ((ControlHandle)item, 1);
  248.     else
  249.         SetControlValue ((ControlHandle)item, 0);
  250.  
  251. // kHasController from hasController
  252.     GetDItem( oneMovieDialog, kHasController, &itemType, &item, &box);
  253.     if ((*theOptions).hasController)
  254.         SetControlValue ((ControlHandle)item, 1);
  255.     else
  256.         SetControlValue ((ControlHandle)item, 0);
  257.  
  258. // kDoTheLoop from do20to10Loop
  259.     GetDItem( oneMovieDialog, kDoTheLoop, &itemType, &item, &box);
  260.     if ((*theOptions).do20to10Loop)
  261.         SetControlValue ((ControlHandle)item, 1);
  262.     else
  263.         SetControlValue ((ControlHandle)item, 0);
  264.  
  265. // kRateChangePU from rateChangeDelay
  266.     GetDItem( oneMovieDialog, kRateChangePU, &itemType, &item, &box);
  267.     newPUValue = ConvertTimeToMenuItem( (*theOptions).rateChangeDelay );
  268.     SetControlValue ((ControlHandle)item, newPUValue);
  269.  
  270. // kSlaveAheadPU from slaveAheadBy
  271.     GetDItem( oneMovieDialog, kSlaveAheadPU, &itemType, &item, &box);
  272.     newPUValue = ConvertTimeToMenuItem( (*theOptions).slaveAheadBy );
  273.     SetControlValue ((ControlHandle)item, newPUValue);
  274. // If we're not setting up a master/slave them we don't need
  275. // this , so gray it out
  276.     if (!hasSlaveMovie)
  277.         HiliteControl ((ControlHandle)item, 255);
  278.  
  279. // Set slave start delay PU
  280.     GetDItem( oneMovieDialog, kSlaveDelayStartPU, &itemType, &item, &box);
  281.     newPUValue = ConvertTimeToMenuItem( (*theOptions).slaveStartDelay );
  282.     SetControlValue ((ControlHandle)item, newPUValue);
  283. // If we're not setting up a master/slave them we don't need
  284. // this , so gray it out
  285.     if (!hasSlaveMovie)
  286.         HiliteControl ((ControlHandle)item, 255);
  287.  
  288. }
  289.  
  290. //----------------------------------------------
  291. // GetOneMovieDLOG
  292. //
  293. // Gets the information from the items in the preferences
  294. // dialog and puts them back in "MovieOptionsType *theOptions"
  295. // In general, each chunk get's the control handle from
  296. // a dialog item and interprates the result accordingly
  297. //----------------------------------------------
  298.  
  299. void GetOneMovieDLOG( DialogPtr oneMovieDialog,
  300.                                             MovieOptionsType *theOptions );
  301.  
  302. void GetOneMovieDLOG( DialogPtr oneMovieDialog,
  303.                                             MovieOptionsType *theOptions )
  304. {
  305.     short  itemType;
  306.     Handle item;
  307.     Rect   box;
  308.     short  newPUValue;
  309.  
  310. // kCloseAtEnd into closeAtEnd
  311.     GetDItem( oneMovieDialog, kCloseAtEnd, &itemType, &item, &box);
  312.     (*theOptions).closeAtEnd = (GetControlValue ((ControlHandle)item) == 1);
  313.  
  314. // kHasController into hasController
  315.     GetDItem( oneMovieDialog, kHasController, &itemType, &item, &box);
  316.     (*theOptions).hasController = (GetControlValue ((ControlHandle)item) == 1);
  317.  
  318. // kDoTheLoop into do20to10Loop
  319.     GetDItem( oneMovieDialog, kDoTheLoop, &itemType, &item, &box);
  320.     (*theOptions).do20to10Loop = (GetControlValue ((ControlHandle)item) == 1);
  321.  
  322. // kRateChangePU into rateChangeDelay
  323.     GetDItem( oneMovieDialog, kRateChangePU, &itemType, &item, &box);
  324.     newPUValue = GetControlValue ((ControlHandle)item);
  325.     (*theOptions).rateChangeDelay = ConvertMenuItemToTime( newPUValue );
  326.  
  327. // kSlaveAheadPU into slaveAheadBy
  328.     GetDItem( oneMovieDialog, kSlaveAheadPU, &itemType, &item, &box);
  329.     newPUValue = GetControlValue ((ControlHandle)item);
  330.     (*theOptions).slaveAheadBy = ConvertMenuItemToTime( newPUValue );
  331.  
  332. // kSlaveDelayStartPU into slaveStartDelay
  333.     GetDItem( oneMovieDialog, kSlaveDelayStartPU, &itemType, &item, &box);
  334.     newPUValue = GetControlValue ((ControlHandle)item);
  335.     (*theOptions).slaveStartDelay = ConvertMenuItemToTime( newPUValue );
  336. }
  337.  
  338. //----------------------------------------------
  339. // GetItemHelpText
  340. //
  341. // Item hit gives the last item clicked on, interprates
  342. // its current state and returns an appropriate comment
  343. // in "helpString"
  344. //----------------------------------------------
  345.  
  346. void GetItemHelpText( DialogPtr oneMovieDialog,
  347.                                             Str255    helpString,
  348.                                             short     itemHit );
  349.  
  350. void GetItemHelpText( DialogPtr oneMovieDialog,
  351.                                             Str255    helpString,
  352.                                             short     itemHit )
  353. {
  354.     short   itemType;
  355.     Handle  item;
  356.     Rect    box;
  357.     short   strNum = 1; // Resource index of the string we want
  358.     
  359.     
  360. // Get information about the last dialog item clicked on
  361.     GetDItem( oneMovieDialog, itemHit, &itemType, &item, &box);
  362.  
  363. // Look at what was last hit and what it's state is
  364.     switch (itemHit)
  365.     {
  366.     case (kCloseAtEnd) :
  367.         if (GetControlValue ((ControlHandle)item))
  368.             strNum = 2;
  369.         else
  370.             strNum = 3;
  371.         break;
  372.     case (kHasController) :
  373.         if (GetControlValue ((ControlHandle)item))
  374.             strNum = 4;
  375.         else
  376.             strNum = 5;
  377.         break;
  378.     case (kDoTheLoop) :
  379.         if (GetControlValue ((ControlHandle)item))
  380.             strNum = 6;
  381.         else
  382.             strNum = 7;
  383.         break;
  384.     case (kRateChangePU) :
  385.         strNum = GetControlValue ((ControlHandle)item) + 7;
  386.         break;
  387.     case (kSlaveAheadPU) :
  388.         strNum = GetControlValue ((ControlHandle)item) + 10;
  389.         break;
  390.     case (kSlaveDelayStartPU) :
  391.         strNum = GetControlValue ((ControlHandle)item) + 13;
  392.         break;
  393.     };
  394.  
  395. // Now get helpString from the resource file
  396.     GetIndString ( helpString, kOneMovieID, strNum);
  397. }
  398.  
  399. //----------------------------------------------
  400. // OneMoviePref
  401. //
  402. // This creates, handles and destroys the movie
  403. // preferences dialog box
  404. //----------------------------------------------
  405.  
  406. OSErr OneMoviePref( MovieOptionsType *theOptions,
  407.                                         Boolean hasSlaveMovie )
  408. {
  409.     OSErr     theErr = noErr;
  410.     GrafPtr   savePort = nil;
  411.     DialogPtr oneMovieDialog;
  412.     ModalFilterUPP theFilter = nil;
  413.     short     itemHit = 0;  // Item clicked by user
  414.     short     lastHit = 0;  // Last item clicked by user
  415.     Str255    helpString;   // help String
  416.     short     itemType;
  417.     Handle    item;
  418.     Rect      box;
  419.     short     checkBox;  //Temporary value of a checkbox control
  420.     
  421.   GetPort(&savePort);
  422.  
  423. // Load the resource from the res file
  424.     oneMovieDialog = GetNewDialog(kOneMovieID, nil, (WindowPtr) -1 );
  425.  
  426. // Setup the dialogs items
  427.     SetUpOneMovieDLOG( oneMovieDialog, theOptions, hasSlaveMovie );
  428.     
  429. // Show the dialog to the user now it's set up
  430.   SetPort( oneMovieDialog );
  431.     ShowWindow( oneMovieDialog );
  432.         
  433. // Get the standard filter proc
  434.   if (GetStdFilterProc(&theFilter) != noErr)
  435.       DebugStr("\pFailed to get standard dialog filter.");
  436.   
  437. // Tell the OS which items the <OK> and <Cancel> buttons are
  438.     SetDialogDefaultItem( oneMovieDialog, kOkButton);
  439.     SetDialogCancelItem ( oneMovieDialog, kCancelButton);
  440.   
  441. // Modal dialog loop
  442.     do
  443.     {
  444. // Use "theFilter" in ModalDialog call
  445.        ModalDialog(theFilter,&itemHit);
  446.  
  447.         switch (itemHit)
  448.         {
  449. // Toggle Check box type items
  450.             case (kCloseAtEnd) :
  451.             case (kHasController) :
  452.             case (kDoTheLoop) :
  453.     // Get the control handle - in item
  454.                 GetDItem( oneMovieDialog, itemHit, &itemType, &item, &box);
  455.     // Get the current state of the check box
  456.                 checkBox = GetControlValue ((ControlHandle)item);
  457.     // Toggle it's state
  458.                 checkBox = 1 - checkBox;
  459.     // Put the new item into the control
  460.                 SetControlValue ((ControlHandle)item, checkBox);
  461.                 break;
  462.         }
  463.  
  464. // Put up the help text
  465.         if ((itemHit != lastHit) && (itemHit > 0))
  466.         {
  467.     // Work out what text we're going to put up
  468.             GetItemHelpText( oneMovieDialog, helpString, itemHit );
  469.     //  and put it into the static text item 
  470.             GetDialogItem ( oneMovieDialog, kHelpText, &itemType, &item, &box);
  471.             SetDialogItemText (item, helpString);
  472.         }
  473.     } while ((itemHit != kOkButton) && (itemHit != kCancelButton));
  474. // Keep going until the <OK> and <Cancel> have been hit
  475.  
  476. // If the <OK> button was pressed then extract the information
  477. // from the dialog items, and return noErr
  478.     if (itemHit == kOkButton)
  479.     {
  480.         GetOneMovieDLOG( oneMovieDialog, theOptions );
  481.         theErr = noErr;
  482.     }
  483.     else
  484. // <Cancel> button was pressed so return userCanceledErr
  485.         theErr = userCanceledErr;
  486.  
  487. // Dispose of the dialog and reset the port
  488.   DisposeDialog(oneMovieDialog);
  489.   SetPort(savePort);
  490.   
  491.   return ( theErr );
  492. }
  493.  
  494.